package com.netease.xone.adapter;

import java.util.HashMap;

import protocol.meta.Source;
import protocol.meta.Subject;
import protocol.meta.User;
import Utils.StackTraceUtil;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;

import com.google.gson.Gson;
import com.netease.log.NTLog;
import com.netease.xone.itemview.ItemViewTipBase;
import com.netease.xone.itemview.ItemViewTipSource;
import com.netease.xone.itemview.ItemViewTipSubject;
import com.netease.xone.itemview.ItemViewTipTopic;
import com.netease.xone.itemview.ItemViewTipUser;

import db.manager.ManagerDiscussTip;


public class AdapterTip extends CursorAdapter {
    private static final String TAG = AdapterTip.class.getSimpleName();

    private int mType = -1;
    // 用于首字母排序，不是每种情况都用
    private AlphabetIndexer mAlphaIndex = null;
    // 支持多选，保持好友选中信息
    private HashMap<Integer, String> mHashMapUser = new HashMap<Integer, String>();
    // 支持多选，保持订阅源选中信息
    private HashMap<Integer, String> mHashMapSource = new HashMap<Integer, String>();

    public AdapterTip(Context context, Cursor c, int type) {
        super(context, c, false);
        this.mType = type;
    }

    public void setAlphabetIndexer(AlphabetIndexer alpha) {
        this.mAlphaIndex = alpha;
    }

    public HashMap<Integer, String> getCheckHashMapUser() {
        return mHashMapUser;
    }
    
    public HashMap<Integer, String> getCheckHashMapSource() {
        return mHashMapSource;
    }
    
    @Override
    public Object getItem(int position) {
        Object obj = null;
        Cursor cursor = getCursor();
        if (cursor.isClosed()) {
        	return obj;
        }
        cursor.moveToPosition(position);
        String value = cursor.getString(ManagerDiscussTip.JSON);
        Gson gson = new Gson();
        switch (mType) {
            case ItemViewTipBase.TYPE_USER:
                obj = gson.fromJson(value, User.class);
                break;
            case ItemViewTipBase.TYPE_SOURCE:
                obj = gson.fromJson(value, Source.class);
                break;
            case ItemViewTipBase.TYPE_TOPIC:
                obj = cursor.getString(ManagerDiscussTip.CONTENT);
                break;
            case ItemViewTipBase.TYPE_SUBJECT:
                obj = gson.fromJson(value, Subject.class);
                break;
            default:
                break;
        }

        return obj;
    }

    @Override
    public void bindView(View convertView, Context context, Cursor cursor) {
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " position=" + cursor.getPosition());
        if (convertView instanceof ItemViewTipBase) {
            ((ItemViewTipBase)convertView).bindView(cursor);
            ((ItemViewTipBase)convertView).bindView(cursor, mAlphaIndex);
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " position=" + cursor.getPosition());
        View view = null;

        switch (mType) {
            case ItemViewTipBase.TYPE_USER:
                view = new ItemViewTipUser(context, mType);
                ((ItemViewTipBase)view).setCheckArray(mHashMapUser);
                break;
            case ItemViewTipBase.TYPE_SOURCE:
                view = new ItemViewTipSource(context, mType);
                ((ItemViewTipBase)view).setCheckArray(mHashMapSource);
                break;
            case ItemViewTipBase.TYPE_TOPIC:
                view = new ItemViewTipTopic(context, mType);
                break;
            case ItemViewTipBase.TYPE_SUBJECT:
                view = new ItemViewTipSubject(context, mType);
                break;
            default:
                break;
        }

        return view;
    }

}
